# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Maintainer: Francesco Colista <fcolista@alpinelinux.org>
pkgname=lxd-feature
pkgver=5.20
pkgrel=0
pkgdesc="Container hypervisor and a new user experience for LXC - 'Feature' release channel"
url="https://ubuntu.com/lxd"
arch="all"
license="AGPL-3.0"
pkggroups="lxd"
depends="
	acl
	attr
	ca-certificates
	cgmanager
	dnsmasq
	iproute2
	iptables
	lxc
	netcat-openbsd
	rsync
	shadow-subids
	squashfs-tools
	tar
	xz
	"
makedepends="
	acl-dev
	acl-static
	autoconf
	automake
	dqlite-dev
	dqlite-static
	eudev-dev
	gettext-dev
	gettext-static
	go
	intltool
	libcap-dev
	libcap-static
	libseccomp-static
	libtool
	libuv-dev
	libuv-static
	linux-headers
	lxc-dev
	lz4-dev
	patchelf
	raft-dev
	raft-static
	sqlite-dev
	sqlite-static
	tcl-dev
	zlib-static
	"
subpackages="
	$pkgname-scripts:scripts:noarch
	$pkgname-bash-completion
	$pkgname-openrc
	$pkgname-doc
	"
install="$pkgname.pre-install"
options="!check"
source="$pkgname-$pkgver.tar.gz::https://github.com/canonical/lxd/releases/download/lxd-$pkgver/lxd-$pkgver.tar.gz
	$pkgname.confd
	$pkgname.initd
	"
ldpath="/usr/lib/lxd"
sonameprefix="$pkgname:"
builddir="$srcdir/lxd-$pkgver"

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

build() {
	export GOPATH="$builddir/_dist"
	export CGO_LDFLAGS="-lintl $LDFLAGS"
	export CGO_LDFLAGS_ALLOW="(-Wl,-wrap,pthread_create)|(-Wl,-z,now)"
	export GO111MODULE=on

	mkdir -p bin
	go build -v -tags "netgo libsqlite3" -ldflags '-extldflags "-static -lm -ldl -lz -lpthread -lz -lintl -lraft -ldqlite -luv -lseccomp -lcap"' -o bin/ ./lxd-migrate/...
	go build -v -tags "agent libsqlite3" -ldflags '-extldflags "-static -lm -ldl -lz -lpthread -lz -lintl -lraft -ldqlite -luv -lseccomp -lcap"' -o bin/ ./lxd-agent/...

	for tool in fuidshift lxc lxc-to-lxd lxd lxd-benchmark; do
		go build -v -tags "libsqlite3" -o bin/ ./$tool/...
	done
}

package() {
	for tool in lxc fuidshift lxc-to-lxd lxd lxd-benchmark lxd-migrate lxd-agent; do
		install -p -Dm755 "bin/$tool" "$pkgdir/usr/bin/$tool"
	done
	install -Dm755 bin/lxd "$pkgdir"/usr/sbin/lxd
	install -Dm755 bin/lxc "$pkgdir"/usr/bin/lxc

	patchelf --set-rpath "/usr/lib/lxd" "$pkgdir/usr/sbin/lxd"

	install -Dm755 "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/lxd
	install -Dm644 "$srcdir"/$pkgname.confd \
		"$pkgdir"/etc/conf.d/lxd

	mkdir -p "$pkgdir"/var/lib/lxd
	chmod 755 "$pkgdir"/var/lib/lxd
	chgrp $pkggroups "$pkgdir"/var/lib/lxd

	install -Dm755 scripts/empty-lxd.sh \
		"$pkgdir"/usr/bin/empty-lxd.sh

	install -Dm644 scripts/bash/lxd-client \
		-t "$pkgdir"/usr/share/bash-completion/completions/

	mkdir -p "$pkgdir/usr/share/doc/lxd"
	cat > "$pkgdir"/usr/share/doc/lxd/README.alpine <<-EOF
	-----------------------------------
	Be sure to add your local user to the lxd group.
	EOF
}

scripts() {
	pkgdesc="LXD scripts"
	depends="$pkgname=$pkgver-r$pkgrel py3-lxc jq"

	amove usr/bin/empty-lxd.sh
}

sha512sums="
cf593f967d9d595577ab9b05bad99d04b9c30031461fdd4769f4746e651b3aa8e94fca53548da1aec22ff312a91ff46710ab88043a633fbd78ffafe130fed3e3  lxd-feature-5.20.tar.gz
1bbb26a61b3812e6eb4c3cb7db6c2d9adb43195f96f317d6bba1ace6a97f1faed0677a12c3827002bc147edba9b355f0e7ead3960d254a131b25fb8c060ea8d0  lxd-feature.confd
ebf9608ea3db25b456a557c81838c6a793adf5f490bd64e1f3dc6951bad619188cb0170f0a794b086adbd128267b4339ab46c1b6a815a4ae7f3a6566b7854d97  lxd-feature.initd
"
